/*
 * Copyright 2014 HarryKim.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package opera.channel.handler;

import client.MapleCharacter;
import client.MapleClient;
import client.Skill;
import client.SkillFactory;
import client.inventory.Item;
import constants.GameConstants;
import java.util.Map;
import server.MapleInventoryManipulator;
import server.MapleItemInformationProvider;
import server.Randomizer;
import tools.MaplePacketCreator;
import tools.data.LittleEndianAccessor;

/**
 *
 * @author HarryKim
 */
public class SkillLearnHandler {

    public static void UseSkillLearnItem(LittleEndianAccessor reader, MapleClient client) {
        MapleCharacter gc = client.getPlayer();
        gc.updateTick(reader.readInt());
        short itemSlot = reader.readShort();
        int itemID = reader.readInt();
        applySkillLearnItem(itemSlot, itemID, client, gc);
    }

    public static void applySkillLearnItem(short itemSlot, int itemID, MapleClient client, MapleCharacter gc) {
        Item item = gc.getInventory(GameConstants.getInventoryType(itemID)).getItem(itemSlot);
        if (item == null) {
            return;
        }
        if (item.getQuantity() < 1 || item.getItemId() != itemID || gc.hasBlockedInventory()) {
            return;
        }
        Map<String, Integer> itemData = MapleItemInformationProvider.getInstance().getEquipStats(itemID);
        if (itemData == null) {
            return;
        }
        boolean masteryBook = GameConstants.isMasteryBook(itemID);
        Integer skillID = null;
        Integer successRate = itemData.get("success");
        Integer masterLevel = itemData.get("masterLevel");
        Integer reqSkillLevel = itemData.get("reqSkillLevel");
        boolean success = false;
        boolean canUse = false;
        int i = 0;
        while (true) {
            skillID = itemData.get("skillid" + i);
            i++;
            if (skillID == null) {
                break;
            }
            Skill skill = SkillFactory.getSkill(skillID);
            if (skill == null) {
                continue;
            }
            if (skill.canBeLearnedBy(gc.getJob())
                    && (reqSkillLevel == null || gc.getSkillLevel(skill) >= reqSkillLevel)
                    && gc.getMasterLevel(skill) < masterLevel) {
                canUse = true;
                if (successRate == null || Randomizer.nextInt(100) <= successRate) {
                    success = true;
                    gc.changeSkillLevel(skill, gc.getSkillLevel(skill), (byte) (int) masterLevel);
                }
                MapleInventoryManipulator.removeFromSlot(client, GameConstants.getInventoryType(itemID), itemSlot, (short) 1, false);
                break;
            }
        }
        gc.getMap().broadcastMessage(MaplePacketCreator.SkillLearnItemResult(gc, masteryBook, canUse, success));
        client.getSession().write(MaplePacketCreator.enableActions());
    }
}
